package Demo4;

public class Main {

}

class MyClockingQueue {
    private static String[] queue = new String[1000];
    private volatile int front = 0;

    private volatile int tail = 0;

    private volatile int size = 0;

    public void put(String s) throws InterruptedException {
        synchronized (this) {
            while (size == queue.length) {
                this.wait();
            }
            queue[tail++] = s;
            if (tail == queue.length) {
                tail = 0;
            }
            size++;
            this.notify();
        }
    }

    public String take() throws InterruptedException {
        synchronized (this) {
            while (size == 0) {
                this.wait();
            }
            String s = queue[front];
            front++;
            if (front == queue.length) {
                front = 0;
            }
            size--;
            this.notify();

            return s;
        }
    }

}